#!/bin/env python
'''
Created on Jan 31, 2011

@author: alex
'''


import sys
from os import path
from jobDispatcher import dispatch
from jobDispatcher.dispatcher  import getHostDispatcher
from util import formatTimeStr, day

# Note: to use resubmit.py:
# resubmit.py --nCpu N --walltime days:hours:minutes:seconds ABSOLUTE_JOBFOLDER



argL = sys.argv[1:]

folder = argL.pop() # last arg is the folder

nCpu = None
walltime = None


for i,arg in enumerate(argL):
    if arg == '--nCpu':
        nCpu = int(argL[i+1])
    if arg == '--walltime':
        walltime = formatTimeStr(argL[i+1])

if nCpu is not None:
    assert nCpu < 10000, "don't you think you are exaggerating ..."
    assert nCpu > 0, "dont't try to fool me..."
    
if walltime is not None:
    assert walltime > 0, "Negative walltime is impossible."
    assert walltime < 365*day, "excessive walltime."
    
if not path.exists(folder):
    print "Job root %s doesn't exist"%folder
    sys.exit(1)

jobTree = dispatch.JobTree(folder)

# reconfigure nCpu and walltime
if nCpu is not None or walltime is not None:
    for job in jobTree.getJobList():    
        job.conf.nCpu = nCpu
        job.conf.walltime = walltime
        job.saveConf()
    
dispatcher = getHostDispatcher()

# by default does not resubmit jobs that have finished with no error
dispatcher.submitTree(jobTree)

    

     
